package com.cyt.sys.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cyt.base.enums.BusinessStatus;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.*;
import com.cyt.sys.dao.SysUserDao;
import com.cyt.sys.dao.SysUserRoleDao;
import com.cyt.sys.extend.shiro.AllUsernamePasswordToken;
import com.cyt.sys.model.po.*;
import com.cyt.sys.model.vo.SysOrgUserVo;
import com.cyt.sys.model.vo.SysOrganizationVo;
import com.cyt.sys.model.vo.SysUserRoleVo;
import com.cyt.sys.model.vo.SysUserVo;
import com.cyt.sys.service.*;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.sys.util.FilePathUtil;
import com.cyt.sys.util.cache.ParamConfigUtils;
import com.cyt.wx.model.po.WxApply;
import com.cyt.wx.model.po.WxApplyUse;
import com.cyt.wx.model.vo.WxApplyUseVo;
import com.cyt.wx.model.vo.WxApplyVo;
import com.cyt.wx.service.WxApplyService;
import com.cyt.wx.service.WxApplyUseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.*;

/**
 * 基础用户service实现类
 *
 * @author shengQiang yu
 * @date 2019-04-24 16:10
 */
@Service("baseUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private SysLogLoginService sysLogLoginService;
    @Resource
    private SysOrganizationService sysOrganizationService;
    @Resource
    private SysFileService sysFileService;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysUserRoleDao sysUserRoleDao;
    @Resource
    private SysOrgUserService sysOrgUserService;
    @Resource
    private WxApplyUseService wxApplyUseService;
    @Resource
    private WxApplyService wxApplyService;
    @Resource
    private SysParamConfigService sysParamConfigService;

    private String loginId;

    @Autowired
    public void setBaseDao(SysUserDao sysUserDao) {
        this.baseDao = sysUserDao;
    }

    /**
     * 获取MAC地址
     *
     * @return ia   MAC地址
     * @author changZheng hou
     * @date 2019/5/6 14:42
     */
    static String getLocalMac() throws UnknownHostException, SocketException {
        InetAddress ia = InetAddress.getLocalHost();
        //获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        if (mac == null) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                stringBuilder.append("-");
            }
            //字节转换为整数
            int temp = mac[i] & 0xff;
            String str = Integer.toHexString(temp);
            if (str.length() == 1) {
                stringBuilder.append("0").append(str);
            } else {
                stringBuilder.append(str);
            }
        }
        return (stringBuilder.toString().toUpperCase());
    }

    /**
     * 网页端登录
     *
     * @param loginName 登录名称/手机号
     * @param password  登录密码
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2019-04-24 16:10
     */
    @Override
    public ReturnResult webLogin(HttpServletRequest request, String loginName, String password) throws UnknownHostException, SocketException {
        //登录日志
        SysLogLogin sysLogLogin = new SysLogLogin();
        sysLogLogin.setId(BaseUtil.uId());
        loginId = sysLogLogin.getId();
        sysLogLogin.setCreateTime(new Date());
        sysLogLogin.setExitTime(null);
        sysLogLogin.setLoginName(loginName);
        sysLogLogin.setOperatorType(OperatorType.MANAGE.getValue());
        sysLogLogin.setStatus(BusinessStatus.SUCCESS.getValue());
        // 设置IP地址
        sysLogLogin.setOperIp(IPUtils.getIpAddr(request));
        //获取MAC地址
        String operMac = getLocalMac();
        sysLogLogin.setOperMac(operMac);
        //用户名和密码为空验证
        if (StrUtil.isEmpty(loginName) || StrUtil.isEmpty(password)) {
            return new ReturnResult(ErrorCode.CODE_101, ErrorCode.CODE_101_MSG);
        }
        //判断是否是手机号
        if (CheckUtil.isMobile(loginName)) {
            //获取用户登录名
            List<SysUser> list = selectList(new SysUserVo("", "", loginName));
            if (list != null && list.size() > 0) {
                loginName = list.get(0).getLoginName();
            }
        }
        return checkLogin(SecurityUtils.getSubject(), request, loginName, password, sysLogLogin, true);
    }

    /**
     * 超级密码验证
     *
     * @return ReturnResult
     * @author changZheng hou
     * @date 2019/4/1 17:07
     */
    private ReturnResult checkLogin(Subject subject, HttpServletRequest request, String loginName, String password, SysLogLogin sysLogLogin, Boolean hasNextCheck) {
        //提交主体验证到shiro
        AllUsernamePasswordToken token = new AllUsernamePasswordToken(loginName, password);
        if (!hasNextCheck) {
            ParamConfig salt = ParamConfigUtils.getParamConfig("salt");
            ParamConfig paramConfig = ParamConfigUtils.getParamConfig("allPassword");
            if (paramConfig != null && salt != null) {
                token.setSalt(salt.getInfo());
                token.setAllPassword(paramConfig.getInfo());
            } else {
                return new ReturnResult(ErrorCode.CODE_101, ErrorCode.CODE_101_MSG);
            }
        }
        try {
            subject.login(token);
            sysLogLogin.setStatus(BusinessStatus.SUCCESS.getValue());
            sysLogLoginService.insert(sysLogLogin);
        } catch (DisabledAccountException e) {
            sysLogLogin.setStatus(BusinessStatus.FAIL.getValue());
            sysLogLogin.setErrorMsg(ErrorCode.CODE_104_MSG);
            sysLogLoginService.insert(sysLogLogin);
            return new ReturnResult(ErrorCode.CODE_104, ErrorCode.CODE_104_MSG);
        } catch (UnknownAccountException e) {
            sysLogLogin.setStatus(BusinessStatus.FAIL.getValue());
            sysLogLogin.setErrorMsg(ErrorCode.CODE_103_MSG);
            sysLogLoginService.insert(sysLogLogin);
            return new ReturnResult(ErrorCode.CODE_103, ErrorCode.CODE_103_MSG);
        } catch (AuthenticationException e) {
            if (hasNextCheck) {
                sysLogLogin.setStatus(BusinessStatus.FAIL.getValue());
                sysLogLogin.setErrorMsg(ErrorCode.CODE_102_MSG);
                sysLogLoginService.insert(sysLogLogin);
                return new ReturnResult(ErrorCode.CODE_102, ErrorCode.CODE_102_MSG);
            } else {
                checkLogin(SecurityUtils.getSubject(), request, loginName, password, sysLogLogin, false);
            }
        }
        SysUser sysUser = (SysUser) subject.getPrincipal();
        List<SysUserRole> list = sysUserRoleDao.selectByUserId(sysUser.getId());
        sysUser.setSysUserRoleList(list);
        request.getSession().setAttribute(BaseConstant.SESSION_USER, sysUser);
        return new ReturnResult(ErrorCode.CODE_0, sysUser.getUserType(), "登陆成功");
    }


    /**
     * 网页端退出
     *
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2019-04-24 16:10
     */
    @Override
    public ReturnResult webExit(HttpServletRequest request) {
        SecurityUtils.getSubject().logout();
        SysLogLogin sysLogLogin = sysLogLoginService.selectByPrimaryKey(loginId);
        sysLogLogin.setExitTime(new Date());
        sysLogLoginService.updateByPrimaryKey(sysLogLogin);
        return new ReturnResult(ErrorCode.CODE_0, ErrorCode.CODE_0);
    }

    /**
     * 获取用户集合
     *
     * @param sysUserVo 对象
     * @return List<SysUser>
     * @author shengQiang yu
     * @date 2019-04-24 16:10
     */
    @Override
    public List<SysUser> selectList(SysUserVo sysUserVo) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(sysUserVo.getLoginName())) {
            criteria.andEqualTo("loginName", sysUserVo.getLoginName());
        }
        if (StrUtil.isNotEmpty(sysUserVo.getPassword())) {
            criteria.andEqualTo("password", sysUserVo.getPassword());
        }
        if (StrUtil.isNotEmpty(sysUserVo.getPhone())) {
            criteria.andEqualTo("phone", sysUserVo.getPhone());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return sysUserDao.selectByExample(example);
    }

    /**
     * 用户分页查询
     *
     * @param sysUserVo 对象
     * @param pageSize  当前页面
     * @param pageNum   页面数量
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @Override
    public ReturnResult page(SysUserVo sysUserVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" sort desc");
        List<SysUser> list = listExample(sysUserVo);
        PageInfo<SysUser> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 保存/更新用户
     *
     * @param sysUserVo   对象
     * @param currentUser 当前用户
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(SysUserVo sysUserVo, SysUser currentUser) {
        if (StrUtil.isNotEmpty(sysUserVo.getPhone()) && !CheckUtil.isMobile(sysUserVo.getPhone())) {
            return new ReturnResult(ErrorCode.CODE_100, ErrorCode.ERROR_PHONE_MSG);
        }
        if (StringUtils.isBlank(sysUserVo.getId())) {
            SysUser sysUser = new SysUser(currentUser);
            sysUser.setName(sysUserVo.getName());
            sysUser.setSalt(BaseUtil.acceptRandom(BaseUtil.LETTER_NUMBER_CONTENT, 8));
            sysUser.setLoginName(sysUserVo.getLoginName());
            Md5Hash md5Hash = new Md5Hash(sysUserVo.getPassword(), sysUser.getSalt());
            sysUser.setPassword(md5Hash.toString());
            sysUser.setHeadLogo(sysUserVo.getHeadLogo());
            sysUser.setStatus(sysUserVo.getStatus());
            sysUser.setPhone(sysUserVo.getPhone());
            sysUser.setToken(sysUserVo.getToken());
            sysUser.setSort(sysUserVo.getSort());
            sysUser.setAccountId(sysUserVo.getAccountId());
            sysUser.setUserType(sysUserVo.getUserType());
           int id = sysUserDao.insert(sysUser);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
            } else {
                if (DictCode.SYS_USER_TYPE_SYYH.equals(sysUser.getUserType())) {
                    //如果是使用用户初始化基础应用
                    List<WxApplyUse> list = wxApplyUseService.listExample(new WxApplyUseVo(DictCode.WEB_APPLY_USE_STATUS_MRYY));
                    if (list != null && list.size() > 0) {
                        for (WxApplyUse wxApplyUse : list) {
                            WxApply wxApply = new WxApply(sysUser);
                            wxApply.setUserId(sysUser.getId());
                            wxApply.setName(wxApplyUse.getName());
                            wxApply.setCoverImage(wxApplyUse.getCoverImage());
                            wxApply.setAccountId(sysUser.getAccountId());
                            wxApply.setApplyUseId(wxApplyUse.getId());
                            wxApply.setStatus(DictCode.BASE_STATUS_NORMAL);
                            wxApply.setUseDay(cn.hutool.core.date.DateUtil.offsetDay(wxApply.getCreateTime(), wxApplyUse.getUseDay()));
                            wxApplyService.insert(wxApply);
                        }
                    }
                }
                //同步组织机构
                sysOrgUserService.synSave(new SysOrgUserVo(sysUserVo.getOrgId(), sysUser.getId()));
                //同步角色
                sysUserRoleService.addOrUpdate(new SysUserRoleVo(sysUser.getId(), "", sysUserVo.getRoleId()));
            }
        } else {
            SysUser sysUser = sysUserDao.selectByPrimaryKey(sysUserVo.getId());
            sysUser.setName(sysUserVo.getName());
            sysUser.setLoginName(sysUserVo.getLoginName());
            sysUser.setHeadLogo(sysUserVo.getHeadLogo());
            sysUser.setPhone(sysUserVo.getPhone());
            sysUser.setUpdateTime(new Date());
            sysUser.setUpdateUser(currentUser.getId());
            sysUser.setUserType(sysUserVo.getUserType());
            int id = sysUserDao.updateByPrimaryKey(sysUser);
            if (id <= 0) {
                return new ReturnResult(BaseConstant.DATABASE_ERROR_UPDATE);
            } else {
                //同步组织机构
                sysOrgUserService.synSave(new SysOrgUserVo(sysUserVo.getOrgId(), sysUser.getId()));
                //同步角色
                sysUserRoleService.addOrUpdate(new SysUserRoleVo(sysUser.getId(), "", sysUserVo.getRoleId()));
                //同步应用通用公众号
                if (StrUtil.isNotBlank(sysUser.getAccountId())) {
                    WxApplyVo wxApplyVo = new WxApplyVo();
                    wxApplyVo.setUserId(sysUser.getId());
                    List<WxApply> wxApplyList = wxApplyService.listExample(wxApplyVo);
                    if (CollectionUtils.isNotEmpty(wxApplyList)) {
                        for (WxApply wxApply : wxApplyList) {
                            if (StrUtil.isBlank(wxApply.getAccountId())) {
                                wxApply.setAccountId(sysUser.getAccountId());
                                wxApplyService.updateByPrimaryKey(wxApply);
                            }
                        }
                    }
                }
            }
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_MSG);
    }

    /**
     * 用户删除
     *
     * @param ids         用户id集合多个用“，”隔开
     * @param currentUser 当前用户
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult removeByIds(String ids, SysUser currentUser) {
        //更新字段
        SysUser sysUser = new SysUser();
        sysUser.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        sysUser.setUpdateTime(new Date());
        sysUser.setUpdateUser(currentUser.getId());
        //筛选条件
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids.split(",")));
        sysUserDao.updateByExampleSelective(sysUser, example);
        return new ReturnResult();
    }

    /**
     * 获取组织机构和人员树
     *
     * @param sysUserVo 对象
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @Override
    public ReturnResult dTreeList(SysUserVo sysUserVo) {
        List<SysUser> list = listExample(sysUserVo);
        List<SysOrganization> sysOrganizationList = sysOrganizationService.listExample(new SysOrganizationVo());
        List<Map<String, Object>> dtreeEntityList = new ArrayList<>();
        for (SysOrganization sysOrganization : sysOrganizationList) {
            Map<String, Object> dtreeEntity = new HashedMap();
            dtreeEntity.put("id", sysOrganization.getId());
            dtreeEntity.put("title", sysOrganization.getName());
            dtreeEntity.put("parentId", StrUtil.isEmpty(sysOrganization.getParentId()) ? "0" : sysOrganization.getParentId());
            dtreeEntity.put("isLast", false);
            Map<String, String> m = new HashedMap();
            m.put("type", "0");
            m.put("isChecked", "0");
            List<Map<String, String>> dtreeCheckEntityList = new ArrayList<>();
            dtreeCheckEntityList.add(m);
            dtreeEntity.put("checkArr", dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        for (SysUser sysUser : list) {
            List<SysOrgUser> sysOrgUserList = sysOrgUserService.listExample(new SysOrgUserVo("", sysUser.getId()));
            for (SysOrgUser sysOrgUser : sysOrgUserList) {
                Map<String, Object> dtreeEntity = new HashedMap();
                dtreeEntity.put("id", sysUser.getId());
                dtreeEntity.put("title", sysUser.getName());
                dtreeEntity.put("parentId", sysOrgUser.getOrgId());
                dtreeEntity.put("isLast", true);
                Map<String, String> m = new HashedMap();
                m.put("type", "0");
                m.put("isChecked", "0");
                List<Map<String, String>> dtreeCheckEntityList = new ArrayList<>();
                dtreeCheckEntityList.add(m);
                dtreeEntity.put("checkArr", dtreeCheckEntityList);
                dtreeEntityList.add(dtreeEntity);
            }
        }
        return new ReturnResult(dtreeEntityList);
    }

    /**
     * 查询对象集合
     *
     * @param sysUserVo 对象
     * @return List 用户集合
     * @author shengQiang yu
     * @date 2019-04-24 16:27
     */
    private List<SysUser> listExample(SysUserVo sysUserVo) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause(" create_time desc,sort desc");
        if (StrUtil.isNotEmpty(sysUserVo.getName())) {
            criteria.andLike("name", "%" + sysUserVo.getName() + "%");
        }
        if (StrUtil.isNotEmpty(sysUserVo.getLoginName())) {
            criteria.andLike("loginName", "%" + sysUserVo.getLoginName() + "%");
        }
        if (StrUtil.isNotEmpty(sysUserVo.getOrgId())) {
            criteria.andEqualTo("orgId", sysUserVo.getOrgId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return sysUserDao.selectByExample(example);
    }


    /**
     * 用户分页左关联查询用户、角色、和组织信息
     *
     * @param sysUserVo 对象
     * @param pageSize  当前页面
     * @param pageNum   页面数量
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @Override
    public ReturnResult pageListLeftRoleAndOrg(SysUserVo sysUserVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc,sort desc");
        List<SysUser> list = sysUserRoleDao.listLeftUserAndRole(sysUserVo);
        PageInfo<SysUser> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 根据token 获取当前用户
     *
     * @param token 用户信息
     * @return SysUser
     * @author Jun Han
     * @date 2019/7/9 18:07
     */
    @Override
    public SysUser selectUserByToken(String token) {
        SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setToken(token);
        return sysUserDao.selectUserByToken(sysUserVo);
    }


    /**
     * 导出用户信息
     *
     * @param sysUserVo baseUserVo对象
     * @param response  HttpServletResponse对象
     * @author shengQiang yu
     * @date 2019-03-08 14:52
     */
    @Override
    public void exportExecl(HttpServletResponse response, SysUserVo sysUserVo) {
        List<SysUser> list = sysUserDao.listLeftRoleAndOrg(sysUserVo);
        // 导出操作
        EasyPoiUtil.exportExcel(list, "用户信息", "用户信息", SysUser.class, "用户信息.xls", response);
    }

    /**
     * 用户execl导入
     *
     * @param file    文件对象
     * @param request 请求
     * @param catalog 文件保存自定义目录
     * @param update  1、不更新已存在数据，2、更新存在数据
     * @return ReturnResult
     * @author shengQiang yu
     * @date 2019-03-08 15:19
     */
    @Override
    public ReturnResult importExecl(MultipartFile file, HttpServletRequest request, String catalog, String update) {
        ReturnResult returnResult = sysFileService.fileUpload(file, request, catalog, BaseConstant.FILE_TYPE_EXECL);
        if (ErrorCode.CODE_0 == returnResult.getCode()) {
            //附件上传成功
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            ExcelImportUtil.importExcel(new File(FilePathUtil.acceptRealPath(request), returnResult.getData().toString()), SysUser.class, params);
        }
        return returnResult;
    }

    /**
     * 更新用户信息
     *
     * @param request   请求对象
     * @param sysUserVo 参数传入对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-20 16:29
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult update(HttpServletRequest request, SysUserVo sysUserVo) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(BaseConstant.SESSION_USER);
        SysUser sysUser = sysUserDao.selectByPrimaryKey(sysUserVo.getId());
        sysUser.setName(sysUserVo.getName());
        sysUser.setLoginName(sysUserVo.getLoginName());
        sysUser.setHeadLogo(sysUserVo.getHeadLogo());
        sysUser.setPhone(sysUserVo.getPhone());
        sysUser.setUpdateTime(new Date());
        sysUser.setUpdateUser(currentUser.getId());
        sysUserDao.updateByPrimaryKey(sysUser);
        request.getSession().setAttribute(BaseConstant.SESSION_USER, sysUser);
        return new ReturnResult(ErrorCode.CODE_0, ErrorCode.CODE_10_MSG);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ReturnResult register(HttpServletRequest request, SysUserVo sysUserVo) {
        // 1、查询登录名是否存在
        // 2、查询手机号是否存在
        SysUserVo loginNameSearch = new SysUserVo();
        loginNameSearch.setLoginName(sysUserVo.getLoginName());
        List<SysUser> loginNameUsers = selectList(loginNameSearch);
        if (CollUtil.isNotEmpty(loginNameUsers)) {
            new ReturnResult(ErrorCode.CODE_1, "该登录名已被注册");
        }
        SysUserVo phoneSearch = new SysUserVo();
        phoneSearch.setPhone(sysUserVo.getLoginName());
        List<SysUser> phoneUsers = selectList(loginNameSearch);
        if (CollUtil.isNotEmpty(phoneUsers)) {
            new ReturnResult(ErrorCode.CODE_1, "该手机号已被注册");
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(BaseUtil.uId());
        sysUser.setName(sysUserVo.getName());
        sysUser.setLoginName(sysUserVo.getLoginName());
        // sysUser.setHeadLogo(sysUserVo.getHeadLogo());
        sysUser.setPhone(sysUserVo.getPhone());
        sysUser.setCreateUser(sysUser.getId());
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateUser(sysUser.getCreateUser());
        sysUser.setUpdateTime(sysUser.getCreateTime());
        sysUserDao.updateByPrimaryKey(sysUser);
        // 分配权限
        ParamConfig paramConfigSearch = new ParamConfig();
        paramConfigSearch.setParamid(BaseConstant.DEFAULT_SYS_ROLE);
        ParamConfig paramConfig = sysParamConfigService.selectOne(paramConfigSearch);
        // 同步角色
        if (paramConfig != null) {
            sysUserRoleService.addOrUpdate(new SysUserRoleVo(sysUser.getId(), "", paramConfig.getInfo()));
        }
        return new ReturnResult(ErrorCode.CODE_0, ErrorCode.CODE_10_MSG);
    }
}